/**
获取GET方式参数：
	request.getQueryString();
获取POST方式参数：
	request.getInputStream();
	
问题：但是以上两种不通用，而且获取到的参数还需要进一步地解析。

统一方便的获取参数的方式：
 	核心的API：
	request.getParameter("参数名");	根据参数名获取参数值（注意，只能获取一个值的参数）
	request.getParameterNames();	获取所有参数名称列表 
	request.getParameterValues("参数名“)；根据参数名获取参数值（可以获取多个值的参数）

修改POST方式参数编码：
	request.setCharacterEncoding("utf-8");
修改GET方式参数编码：
	手动解码：String name = new String(name.getBytes("iso-8859-1"),"utf-8");


 */

import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RequestParam
 */
@WebServlet("/RequestParam")
public class RequestParam extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RequestParam() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		response.getWriter().append("Served at: ").append(request.getContextPath());
//		System.out.println("GET...");
//		String value = request.getQueryString();
//		System.out.println(value);

//		String username = request.getParameter("username");
//		String password = request.getParameter("password");
//		System.out.println("username: " + username);
//		System.out.println("password: " + password);
		
		System.out.println("---------------------------------");
		System.out.println(request.getMethod() + "方式");
//		request.setCharacterEncoding("utf-8");	//解决编码问题
		Enumeration<String> pNames = request.getParameterNames();
		while (pNames.hasMoreElements()) {
			String name = (String) pNames.nextElement();
//			if ("GET".equals(request.getMethod())) {
//				name = new String(name.getBytes("iso-8859-1"), "utf-8");
//			}
			//这里需要考虑编码问题
			//方法一：用原编码重新编码（"iso-8859-1"）
			//name = new String(name.getBytes("iso-8859-1"), "utf-8");
			//方法二：设置默认编码（只对 POST 方式有效）
			//request.setCharacterEncoding("utf-8");
			if ("hobit".equals(name)) {
				String[] hobits = request.getParameterValues(name);
				System.out.print(name + ": ");
				for (String hobit : hobits) {
					System.out.print(hobit + ", ");
				}
				System.out.println("");
			} else {
				String value = request.getParameter(name);
				System.out.println(name + " : " +value);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		System.out.println("POST...");
		this.doGet(request, response);
////		ServletInputStream in = request.getInputStream();
////		byte[] buf = new byte[1024];
////		int len = -1;
////		while ((len = in.read(buf)) != -1) {
////			System.out.println(new String(buf, 0, len));
////		}
//		
//		String username = request.getParameter("username");
//		String password = request.getParameter("password");
//		System.out.println("username: " + username);
//		System.out.println("password: " + password);
//		
//		System.out.println("---------------------------------");
//		Enumeration<String> pNames = request.getParameterNames();
//		while (pNames.hasMoreElements()) {
//			String name = (String) pNames.nextElement();
//			String value = request.getParameter(name);
//			System.out.println(name + " : " +value);
//		}
	}

}
